﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace PDO.UI.Core
{
    public class BinaryImageConverter : IValueConverter
    {

        static public BitmapImage ConvertFileToImage(string filename)
        {
            return new BitmapImage(new Uri(filename));
        }

        static public System.Data.Linq.Binary ConvertFileToBinary(string filename)
        {
            BitmapImage image = ConvertFileToImage(filename);
            return new System.Data.Linq.Binary(ConvertImageToBytes(image));
        }

        static public byte[] ConvertImageToBytes(BitmapSource image)
        {
            MemoryStream stream = new MemoryStream();
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Interlace = PngInterlaceOption.On;
            encoder.Frames.Add(BitmapFrame.Create(image));
            encoder.Save(stream);

            Byte[] bytBLOBData = new Byte[stream.Length];
            stream.Position = 0;
            stream.Read(bytBLOBData, 0, System.Convert.ToInt32(stream.Length));

            return bytBLOBData;
        }

        static public byte[] ConvertFileToBytes(string filename)
        {
            BitmapImage image = ConvertFileToImage(filename);
            return ConvertImageToBytes(image);
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                byte[] bytes = null;
                if (value is System.Data.Linq.Binary)
                {
                    bytes = (value as System.Data.Linq.Binary).ToArray();
                }
                else if (value is byte[])
                {
                    bytes = value as byte[];
                }

                if (null != bytes)
                {
                    MemoryStream stream;

                    // Check for non-standard encoding used in Northwind sample database images.
                    if (bytes[0] == 21 && bytes[1] == 28 && bytes[2] == 47)
                    {
                        stream = new MemoryStream();
                        int offset = 78;
                        stream.Write(bytes, offset, bytes.Length - offset);
                    }
                    else // normal encoding
                    {
                        stream = new MemoryStream(bytes);
                    }

                    BitmapImage image = new BitmapImage();
                    image.BeginInit();
                    image.StreamSource = stream;
                    image.EndInit();
                    return image;
                }
            }
            return null;
        }


        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is BitmapImage)
            {
                BitmapImage image = value as BitmapImage;

                MemoryStream stream = new MemoryStream();
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Interlace = PngInterlaceOption.On;
                encoder.Frames.Add(BitmapFrame.Create(image));
                encoder.Save(stream);

                Byte[] bytBLOBData = new Byte[stream.Length];
                stream.Position = 0;
                stream.Read(bytBLOBData, 0, System.Convert.ToInt32(stream.Length));
                if (targetType == typeof(System.Data.Linq.Binary))
                {
                    System.Data.Linq.Binary b = new System.Data.Linq.Binary(bytBLOBData);
                    return b;
                }

                return bytBLOBData;
            }
            else
            {
                return null;
            }
        }
    }
}


